﻿<s:SurfaceListBox 
  x:Class="SurfaceCardsGameKit.Mosaic"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:s="http://schemas.microsoft.com/surface/2008"
  xmlns:local="clr-namespace:SurfaceCardsGameKit"
  xmlns:System="clr-namespace:System;assembly=mscorlib" xmlns:Microsoft_Surface_Presentation_Generic="clr-namespace:Microsoft.Surface.Presentation.Generic;assembly=Microsoft.Surface.Presentation.Generic"
  BorderThickness="0"
  x:Name="ListBox" SelectionMode="Multiple" Background="Transparent" ItemContainerStyle="{DynamicResource ListBoxItemStyle1}"
  ScrollViewer.HorizontalScrollBarVisibility="Hidden"
  ScrollViewer.VerticalScrollBarVisibility="Hidden">

  <s:SurfaceListBox.Resources>
    <local:ColorsConverter x:Key="MyColorConverter" />
    <local:ColorsConverter2 x:Key="MyColorConverter2" />
    <local:NumbersConverter x:Key="MyNumberConverter" />
    <local:WrapPanelWidthConverter x:Key="WrapPanelWidthConverter" />
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

    <Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
      <Setter Property="Background" Value="Transparent"/>
      <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
      <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
      <Setter Property="Padding" Value="0"/>
      <Setter Property="Margin" Value="0" />
      <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ListBoxItem}">
            <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0" Padding="0">
              <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
            </Border>
            <ControlTemplate.Triggers>
              <Trigger Property="IsSelected" Value="true">
                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
              </Trigger>
              <MultiTrigger>
                <MultiTrigger.Conditions>
                  <Condition Property="IsSelected" Value="true"/>
                  <Condition Property="Selector.IsSelectionActive" Value="false"/>
                </MultiTrigger.Conditions>
                <Setter Property="Background" TargetName="Bd" Value="#FF0017FF"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
              </MultiTrigger>
              <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
              </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

    <Style x:Key="LabelStyle1" TargetType="{x:Type Label}">
      <!--<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>-->
      <Setter Property="Background" Value="Transparent"/>
      <Setter Property="Padding" Value="2"/>
      <Setter Property="FontWeight" Value="Bold"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
      <Setter Property="FontFamily" Value="Miraiam Fixed"/>

      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type Label}">
            <Border Margin="0,0,-0.833,1.667" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
              <ContentPresenter HorizontalAlignment="Center" Margin="-5" VerticalAlignment="Center" Width="Auto" Height="Auto" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
            </Border>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </s:SurfaceListBox.Resources>
  <s:SurfaceListBox.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel IsItemsHost="True" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type s:SurfaceListBox}}, Path=ActualWidth,
                      Converter={StaticResource WrapPanelWidthConverter}}" />
    </ItemsPanelTemplate>
  </s:SurfaceListBox.ItemsPanel>
  <s:SurfaceListBox.ItemTemplate>
    <DataTemplate>

      <Border>
        <Border.Style>
          <Style TargetType="{x:Type Border}">
            <Setter Property="BorderThickness" Value="0" />
            <Style.Triggers>
              <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type s:SurfaceListBox}}, Path=IsFront}"
                           Value="false">
                <Setter Property="Background" Value="{Binding RelativeSource=
                  {RelativeSource Mode=FindAncestor, AncestorType={x:Type local:DeckUC}}, Path=BackBackground}" />
              </DataTrigger>

              <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type s:SurfaceListBox}}, Path=IsFront}"
                           Value="true">
                <Setter Property="Background" Value="{StaticResource ItemMosaic}" />
              </DataTrigger>
            </Style.Triggers>
          </Style>
        </Border.Style>
        <Grid Width="19"  Height="24" DataContext="{Binding CardValue}">
          <Grid.RowDefinitions>
            <RowDefinition Height="0.6*"/>
            <RowDefinition Height="0.4*"/>
          </Grid.RowDefinitions>

          <Label x:Name="MyLabel" Foreground="{Binding Color, Converter={StaticResource MyColorConverter2}}" Content="{Binding Number, Converter={StaticResource MyNumberConverter}}" Style="{StaticResource LabelStyle1}" Grid.Row="0" 
                           Width="19"
                           Height="11"
               Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type s:SurfaceListBox}}, Path=IsFront,
          Converter={StaticResource BooleanToVisibilityConverter}}" />
          <Rectangle Fill="{Binding Color, Converter={StaticResource MyColorConverter}}" Grid.Row="1" 
                    Width="19"
                    Height="11"
                               VerticalAlignment="Top" 
                   Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type s:SurfaceListBox}}, Path=IsFront,
          Converter={StaticResource BooleanToVisibilityConverter}}"/>
        </Grid>
      </Border>

    </DataTemplate>
  </s:SurfaceListBox.ItemTemplate>
</s:SurfaceListBox>
